home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / M&T Graphics & Sound Examples / Symantec Versions / C02 Sound Playing / P02 Sound Channel Intro / SoundChannelIntro.c next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  3.5 KB  |  146 lines  |  [TEXT/KAHL]

  1. //____________________________________________________________
  2. //    SoundChannelIntro.c
  3. //
  4. //    Copyright © Dan Parks Sydow, 1995
  5. //    From the book:
  6. //    "Graphics and Sound Programming Techniques for the Mac",
  7. //    M&T Books, 1995
  8. //
  9. //    The "Graphics and Sound Programming for the Mac" book shows the call to SndPlay()
  10. //    with a different second parameter than the one used in this code. The book shows the
  11. //    SndPlay() call as expected by the version of the Sound.h universal header file used
  12. //    by Metrowerks at the time of this writing. Symantec uses a different version of 
  13. //    Sound.h. If Symantec updates the Apple Universal Header files, you may get a compilation 
  14. //    error. If you do, it will concern the call to SndPlay() in the PlaySoundResourceSynch() function.
  15. //    Here is the change you'll need to make. Change
  16. //    FROM:
  17. //
  18. //         theError = SndPlay( theChannel, theHandle, false );
  19. //
  20. //    TO:
  21. //
  22. //         theError = SndPlay( theChannel, (SndListHandle)theHandle, false );
  23.  
  24.  
  25.  
  26. //____________________________________________________________
  27.  
  28. #include <Sound.h>
  29.  
  30.  
  31. //____________________________________________________________
  32.  
  33. void           InitializeToolbox( void );
  34. SndChannelPtr  OpenOneSynchSoundChannel( void );
  35. OSErr          DisposeOneSoundChannel( SndChannelPtr );
  36. OSErr          PlaySoundResourceSynch( SndChannelPtr, short );
  37.  
  38.  
  39. //____________________________________________________________
  40.  
  41. #define    rPoliceSiren        9000
  42.  
  43.  
  44. //____________________________________________________________
  45.  
  46. void  main( void )
  47. {
  48.    NumVersion     theSndMgrVers;
  49.    short          theResID;
  50.    OSErr          theError;
  51.    SndChannelPtr  theChannel;
  52.          
  53.    InitializeToolbox();
  54.  
  55.    theSndMgrVers = SndSoundManagerVersion();   
  56.    if ( theSndMgrVers.majorRev < 3 )
  57.       ExitToShell();
  58.    
  59.    theChannel = OpenOneSynchSoundChannel();
  60.    if ( theChannel == nil )
  61.       ExitToShell();
  62.   
  63.    theResID = rPoliceSiren; 
  64.    theError = PlaySoundResourceSynch( theChannel, theResID );
  65.    if ( theError != noErr )
  66.       ExitToShell();
  67.       
  68.    theError = DisposeOneSoundChannel( theChannel );
  69.    if ( theError != noErr )
  70.       ExitToShell();
  71. }
  72.  
  73.  
  74. //____________________________________________________________
  75.  
  76. SndChannelPtr  OpenOneSynchSoundChannel( void )
  77. {
  78.    SndChannelPtr  theChannel;   
  79.    OSErr          theError;
  80.    
  81.    theChannel = nil;
  82.    theError = SndNewChannel( &theChannel, 0, 0, nil );
  83.    
  84.    if ( theError != noErr )
  85.    {
  86.       DisposePtr( (Ptr)theChannel );
  87.       theChannel = nil;
  88.    }
  89.       
  90.    return ( theChannel );
  91. }
  92.  
  93.  
  94. //____________________________________________________________
  95.  
  96. OSErr  DisposeOneSoundChannel( SndChannelPtr theChannel )
  97. {
  98.    OSErr  theError;
  99.    
  100.    theError = SndDisposeChannel( theChannel, true );
  101.    DisposePtr( (Ptr)theChannel );
  102.    
  103.    return ( theError );
  104. }
  105.  
  106.  
  107. //____________________________________________________________
  108.  
  109. OSErr  PlaySoundResourceSynch( SndChannelPtr theChannel, short theResID )
  110. {
  111.    Handle  theHandle;
  112.    OSErr   theError;
  113.    
  114.    theHandle = GetResource( 'snd ', theResID );
  115.    
  116.    if ( theHandle == nil )
  117.    {   
  118.       return ( resProblem );
  119.    }
  120.    else
  121.    {
  122.       HLock( theHandle );
  123.          theError = SndPlay( theChannel, theHandle, false );
  124.       HUnlock( theHandle );
  125.    
  126.       ReleaseResource( theHandle );
  127.  
  128.       return ( theError );
  129.    }
  130. }
  131.  
  132.  
  133. //____________________________________________________________
  134.  
  135. void  InitializeToolbox( void )
  136. {
  137.    InitGraf( &qd.thePort );
  138.    InitFonts();
  139.    InitWindows();
  140.    InitMenus();
  141.    TEInit();
  142.    InitDialogs( 0L );
  143.    FlushEvents( everyEvent, 0 );
  144.    InitCursor();
  145. }
  146.